﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;

namespace Hopeless.Metaphysical.GUI
{
    class WaveCompletedWindow
    {
        private Rectangle _space;
        private Vector2 _messagePosition;
        private string _message;
        private SpriteFont _font;
        private Background _backgroundBlack;
        private Background _backgroundWhite;
        private MenuItem _nextWaveButton;
        private EnhancementControl _criticalControl;
        private EnhancementControl _knockbackControl;
        private EnhancementControl _speedControl;
        private EnhancementControl _protectionControl;

        private bool _nextWaveRequested = false;
        public bool NextWaveRequested
        {
            get { return _nextWaveRequested; }
        }

        private void InitializeMessage()
        {
            var wave = HopelessGame.Universe.CurrentWave;
            _message = string.Format("Wave {0} complete! Choose an enhancement:", wave.Number);

            var size = _font.MeasureString(_message);
            var width = _space.Width;
            var height = _space.Height * 0.1f;
            var x = _space.X + width / 2f - size.X / 2f;
            var y = _space.Y + height / 2f - size.Y / 2f;
            _messagePosition = new Vector2(x, y);
        }

        private void InitializeEnhancementsControls()
        {
            var hero = HopelessGame.Universe.Hero;

            _criticalControl = new EnhancementControl(hero.Critical, GameResources.Content.Load<Texture2D>("Critical"));
            _knockbackControl = new EnhancementControl(hero.Knockback, GameResources.Content.Load<Texture2D>("Knockback"));
            _speedControl = new EnhancementControl(hero.Speed, GameResources.Content.Load<Texture2D>("Speed"));
            _protectionControl = new EnhancementControl(hero.Protection, GameResources.Content.Load<Texture2D>("Protection"));

            var width = _space.Width * 0.5f;
            var height = _space.Height * 0.15f;
            var x = _space.X + _space.Width / 2f - width / 2f;
            var interval = _space.Height * 0.01f;

            _criticalControl.Initialize(new Rectangle((int)x, (int)(_space.Y + _space.Height * 0.1f), (int)width, (int)height));
            _knockbackControl.Initialize(new Rectangle((int)x, (int)(_space.Y + _space.Height * 0.1f + height + interval), (int)width, (int)height));
            _speedControl.Initialize(new Rectangle((int)x, (int)(_space.Y + _space.Height * 0.1f + height * 2 + interval * 2), (int)width, (int)height));
            _protectionControl.Initialize(new Rectangle((int)x, (int)(_space.Y + _space.Height * 0.1f + height * 3 + interval * 3), (int)width, (int)height));

            EventHandler levelRaised = (sender, e) =>
                {
                    _nextWaveButton.Show();
                    _criticalControl.Enabled = _knockbackControl.Enabled = _speedControl.Enabled = _protectionControl.Enabled = false;
                };
            _criticalControl.LevelRaised += levelRaised;
            _knockbackControl.LevelRaised += levelRaised;
            _speedControl.LevelRaised += levelRaised;
            _protectionControl.LevelRaised += levelRaised;
        }

        private void InitializeNextWaveButton()
        {
            var whiteTexture = Texture2DCreator.FromColor(Color.White, GameResources.Graphics.GraphicsDevice);
            var transparentTexture = Texture2DCreator.FromColor(Color.Transparent, GameResources.Graphics.GraphicsDevice);

            _nextWaveButton = new MenuItem("Next Wave", _font);
            _nextWaveButton.ForeColor = Color.White;
            _nextWaveButton.HoveringForeColor = Color.Black;
            _nextWaveButton.Border = new Border(whiteTexture, whiteTexture);
            _nextWaveButton.HoveringBorder = new Border(whiteTexture, whiteTexture);
            _nextWaveButton.BackgroundTexture = transparentTexture;
            _nextWaveButton.BackgroundHoveringTexture = whiteTexture;
            _nextWaveButton.Click += (sender, e) => _nextWaveRequested = true;

            var width = _space.Width * 0.5f;
            var height = _space.Height * 0.15f;
            var x = _space.X + _space.Width / 2f - width / 2f;
            var y = _space.Y + _space.Height * 0.73f + height / 2f;
            _nextWaveButton.Bounds = new Rectangle((int)x, (int)y, (int)width, (int)height);
        }

        public void Initialize(Rectangle space)
        {
            _space = space;
            _font = GameResources.Content.Load<SpriteFont>("Trebuchet MS");

            var backgroundSpace = space;
            var backgroundWhiteTexture = Texture2DCreator.FromColor(Color.White, GameResources.Graphics.GraphicsDevice);
            _backgroundWhite = new Background(backgroundSpace, backgroundWhiteTexture);
            backgroundSpace.Inflate(-2, -2);
            var backgroundBlackTexture = Texture2DCreator.FromColor(Color.Black, GameResources.Graphics.GraphicsDevice);
            _backgroundBlack = new Background(backgroundSpace, backgroundBlackTexture);

            this.InitializeMessage();
            this.InitializeEnhancementsControls();
            this.InitializeNextWaveButton();
        }

        public void Update(GameTime gameTime)
        {
            var mouseState = Mouse.GetState();
            var keyboardState = Keyboard.GetState();

            _criticalControl.Update(gameTime);
            _knockbackControl.Update(gameTime);
            _speedControl.Update(gameTime);
            _protectionControl.Update(gameTime);

            _nextWaveButton.Update(gameTime, mouseState, keyboardState);
        }

        public void Draw(GameTime gameTime)
        {
            _backgroundWhite.Draw(gameTime);
            _backgroundBlack.Draw(gameTime);

            GameResources.SpriteBatch.Begin();
            GameResources.SpriteBatch.DrawString(_font, _message, _messagePosition, Color.White);
            GameResources.SpriteBatch.End();

            _criticalControl.Draw(gameTime);
            _knockbackControl.Draw(gameTime);
            _speedControl.Draw(gameTime);
            _protectionControl.Draw(gameTime);

            _nextWaveButton.Draw(gameTime);
        }
    }
}
